<?php
declare (strict_types = 1);

namespace app\frontend\controller;

use app\frontend\model\Article;
use app\frontend\model\ArticleComment;
use app\frontend\model\ArticleContent;
use app\frontend\model\User;

class GetComment extends Base
{
    // 收到的评论列表
    public function index()
    {
        return view();
    }

    // 收到的评论列表数据接口
    public function list()
    {
        $userInfo = session('userInfo');
        $articleComment = ArticleComment::with(['articleTitle'])->where('author_user_id', $userInfo['id'])
            ->order('update_time', 'desc')
            ->select();
        if (!$articleComment->isEmpty()) {
            $count = ArticleComment::where('user_id', $userInfo['id'])
                ->count();
            $data = $articleComment->toArray();
        } else {
            $count = 0;
            $data = [];
        }

        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $data
        ];

        return json($data);
    }

    // 查看评论
    public function view()
    {
        $id = input('get.id');

        $articleComment = ArticleComment::find($id);
        $article = Article::field('title, user_id')->find($articleComment->article_id);
        $author = User::field('nickname')->find($article->user_id);
        $articleContent = ArticleContent::field('content')
            ->where('article_id', $articleComment->article_id)
            ->find();

        return view('', [
            'articleComment' => $articleComment,
            'articleTitle' => $article->title,
            'articleContent' => $articleContent->content,
            'author' => $author->nickname
        ]);
    }
}
